package com.itheima.BufferedStream.ReaderSortWrite;

import java.io.*;
import java.util.Arrays;

public class BufferedSortWriteDemo {
    public static void main(String[] args) throws IOException {
        //需求：读取文件中的数据，排序后再次写到本地文件
        //1.要把文件中的数据读取进来
        BufferedReader br = new BufferedReader(new FileReader("day623-24CharStream\\sort.txt"));
        String line = br.readLine();
        System.out.println("读取到的数据为" + line);
        br.close();

        //2.按照空格进行切换
        String[] split = line.split(" ");
        //把字符串类型的数组变成int类型
        int[] arr = new int[split.length];
        //遍历split数组，可以进行类型转换
        for (int i = 0; i < split.length; i++) {
            String smallStr = split[i];
            //类型转换
            int number = Integer.parseInt(smallStr);
            //把转换后的结果存入arr中
            arr[i] = number;
        }
        //4.排序
        Arrays.sort(arr);
        System.out.println("排序后：" + Arrays.toString(arr));

        //5.把排序之后结果写回本地
        BufferedWriter bw = new BufferedWriter(new FileWriter("day623-24CharStream\\sort.txt", true));
        //先写一个换行
        bw.newLine();
        //写出
        for (int i = 0; i < arr.length; i++) {
            bw.write(arr[i] + " ");
            bw.flush();
        }
        bw.close();

    }
}
